#include "memory.h"
#include "aic.h"
#include "core.h"
#include "serial.h"
#include "lib.h"
#define VECTOR_TABLE ((volatile unsigned int*)0x20)
#define AIC_VECTOR_TABLE ((volatile unsigned int*)(AIC_BASE + 0x80))
#define MC_BASE (0xFFFFFF00)
#define MC_RCR  (volatile unsigned int*) (MC_BASE + 0x00)
//default Handler für nicht explizit gesetzte Interrupts
static void null_handler();

// Start und Ende der erzeugten Vektortabellenbefehle (siehe vtable_asm.S)
extern long vtable_code_start;
extern long vtable_code_end;

static void null_handler()
{
    disable_irq_in_core();
    kprintf("OHOH, unhandled interrupt\n\r");
    while(1);
}
//------------------------------------------------------------------------------
void remap_memory()
{
  *MC_RCR = (1 << 0); // set remap command bit
}

//------------------------------------------------------------------------------


void init_vector_table()
{
    int i = 0;

    memcpy( 0, &vtable_code_start , 4*8);

    //init global vector table
    for(i = 0; i < 8; i++) VECTOR_TABLE[i] = (volatile unsigned int) null_handler;

    kprintf("Addi vom Nullhandler: %p\n\r", (volatile unsigned int) null_handler);

    for(i = 0; i < 8; i++)
        kprintf("Vektor %i = %p\n\r",i,VECTOR_TABLE[i]);


    kprintf("\nAIC Vektortabelle:\n\r");
    //init AIC vector table
    for(i=0; i < 32; i++) AIC_VECTOR_TABLE[i] = (volatile unsigned int) null_handler;

    for(i = 0; i < 32; i++)
           kprintf("Vektor %i = %p\n\r",i,AIC_VECTOR_TABLE[i]);
    // init BASE_IRQ_HANDLER
    VECTOR_TABLE[SWI] = (volatile unsigned int) &handle_swi_interrupt;
    // init Software Interrupt (SWI)
    AIC_VECTOR_TABLE[IRQ_1 - IRQ_START] = (volatile unsigned int) &handle_sys_interrupt;
}

//------------------------------------------------------------------------------
